package com.kongwg.java;

import org.junit.Test;

import java.io.*;

/**
 * @author kongwg
 * @create 2020-12-18 16:47
 */
public class BufferTest {
    @Test
    public void BufferCopyTest() {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //new file
            File file = new File("爱情与友情.jpg");
            File file1 = new File("爱情与友情3.jpg");

            //new Stream
            FileInputStream fis = new FileInputStream(file);
            FileOutputStream fos = new FileOutputStream(file1);

            //new Buffer
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);

            //operation data
            byte[] buffer = new byte[10];
            int len;//remember data length
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            //trun on stream
            if (bos != null) {

                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis != null) {

                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public void copyFileWrite(String srcPath, String destPath) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //new file
            File file = new File(srcPath);
            File file1 = new File(destPath);

            //new Stream
            FileInputStream fis = new FileInputStream(file);
            FileOutputStream fos = new FileOutputStream(file1);

            //new Buffer
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);

            //operation data
            byte[] buffer = new byte[1024];
            int len;//remember data length
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            //trun on stream
            if (bos != null) {

                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis != null) {

                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @Test
    public void copyFileBufferTest() {
        long start = System.currentTimeMillis();
        String srcPath = "C:\\Users\\Administrator\\Desktop/千与千寻纯音乐.mp3";
        String descPath = "C:\\Users\\Administrator\\Desktop/千与千寻纯音乐test.mp3";
        copyFileWrite(srcPath, descPath);
        long end = System.currentTimeMillis();

        System.out.println("copy time is " + (end - start));

    }


    @Test
    public void BufferedReadWriterTest() {
        //new file
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            File srcPath = new File("dbcp.txt");
            File destPath = new File("dbcp1.txt");
            //new Stream
            FileReader fr = new FileReader(srcPath);
            FileWriter fw = new FileWriter(destPath);
            //new buffer
            br = new BufferedReader(fr);
            bw = new BufferedWriter(fw);

//            //operation data
//            char[] cbuf = new char[10];
//            int len;
//            while ((len = br.read(cbuf)) != -1) {
//                bw.write(cbuf, 0,len);

//            }
            //operation two
            String data;
            while ((data = br.readLine()) != null) {
//                bw.write(data);
//                bw.write(data + "\n");
                //function 2
                bw.write(data);
                bw.newLine();
            }


            System.out.println("copy is sccess");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //close stream
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }


    }
}
